package com.offcn.filter;

import com.offcn.utils.JWTUtil;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @description: TODO
 * @author: LongChen
 * @date: 2022/8/7 17:14
 * @version: 1.0
 */

public class JwtCheckGatewayFilterFactory extends AbstractGatewayFilterFactory<JwtCheckGatewayFilterFactory.Config> {
    // 这里需要将自定义的config传过去，否则会报告ClassCastException
    public JwtCheckGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {

        return new InnerFilter(config);
    }

    //创建一个过滤器实现内部类
    public class InnerFilter implements GatewayFilter, Ordered {
        private Config config;

        //把配置注入构造函数
        InnerFilter(Config config) {
            this.config = config;
        }

        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            //获取请求对象，获取请求头的Authorization 令牌
            String token = exchange.getRequest().getHeaders().getFirst("Authorization");
            //判断令牌如果不为空，
            if (token != null) {
                //使用jwt工具类校验令牌
                boolean verify = JWTUtil.verify(token);
                //如果token校验失败
                if (!verify) {
                    //获取相应对象
                    ServerHttpResponse response = exchange.getResponse();
                    //设置headers
                    HttpHeaders httpHeaders = response.getHeaders();
                    httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
                    httpHeaders.add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
                    //设置body
                    String warningStr = "token验证失败";
                    DataBuffer bodyDataBuffer = response.bufferFactory().wrap(warningStr.getBytes());
                    return response.writeWith(Mono.just(bodyDataBuffer));
                } else {
                    //放行
                    return chain.filter(exchange);
                }
            } else {
                //获取相应对象
                ServerHttpResponse response = exchange.getResponse();
                //设置headers
                HttpHeaders httpHeaders = response.getHeaders();
                httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
                httpHeaders.add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
                //设置body
                String warningStr = "token不存在";
                DataBuffer bodyDataBuffer = response.bufferFactory().wrap(warningStr.getBytes());
                return response.writeWith(Mono.just(bodyDataBuffer));
            }
        }

        //配置过滤器实现顺序
        @Override
        public int getOrder() {
            return 0;
        }
    }

    //创建一个内部类，配置过滤器属性
    public static class Config {

    }
}
